package leetcode.editor.cn;//给你一个由 n 个整数组成的数组 nums ，和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[
//b], nums[c], nums[d]] （若两个四元组元素一一对应，则认为两个四元组重复）： 
//
// 
// 0 <= a, b, c, d < n 
// a、b、c 和 d 互不相同 
// nums[a] + nums[b] + nums[c] + nums[d] == target 
// 
//
// 你可以按 任意顺序 返回答案 。 
//
// 
//
// 示例 1： 
//
// 
//输入：nums = [1,0,-1,0,-2,2], target = 0
//输出：[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
// 
//
// 示例 2： 
//
// 
//输入：nums = [2,2,2,2,2], target = 8
//输出：[[2,2,2,2]]
// 
//
// 
//
// 提示： 
//
// 
// 1 <= nums.length <= 200 
// -10⁹ <= nums[i] <= 10⁹ 
// -10⁹ <= target <= 10⁹ 
// 
//
// Related Topics 数组 双指针 排序 👍 1685 👎 0


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution18 {
    public static void main(String[] args) {
        Solution18 solution18 = new Solution18();
        String param = "0,0,0,1000000000,1000000000,1000000000,1000000000";
        Integer tar = 1000000000;
        String[] split = param.split(",");
        int[] arr = new int[split.length];
        for(int i = 0;i<split.length;i++){
            arr[i] = Integer.valueOf(split[i]);
        }
        List<List<Integer>> lists = solution18.fourSum(arr, tar);
        System.out.println(lists);
    }
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> rest = new ArrayList<>();
        for(int i = 0;i<nums.length-3;i++){
            if(i > 0 && nums[i] == nums[i-1]){
                continue;
            }
            if((long)((long)nums[i] + (long)nums[i+1] + (long)nums[i+2] + (long)nums[i+3]) > target){
                break;
            }
            if((long)((long)nums[i] + (long)nums[nums.length-3] + (long)nums[nums.length-2] + (long)nums[nums.length-1]) < target){
                continue;
            }
            long tar = target - nums[i];
            for(int j = i+1;j<nums.length-2;j++){
                if(j > i+1 && nums[j] == nums[j-1]){
                    continue;
                }
                if((long)((long)nums[i] + (long)nums[j] + (long)nums[j+1] + (long)nums[j+2]) > target){
                    break;
                }
                if((long)((long)nums[i] + (long)nums[j] + (long)nums[nums.length-2] + (long)nums[nums.length-1]) < target){
                    continue;
                }
                long ta2 = tar - nums[j];
                int t = nums.length-1;
                int f= j+1;
                while (f < t){
                    if(nums[f] + nums[t] > ta2){
                        t--;
                        while (nums[t] == nums[t+1]){
                            t--;
                        }
                    }else if(nums[f] + nums[t] < ta2){
                        f++;
                        while (nums[f] == nums[f-1]){
                            f++;
                        }
                    }else {
                        List<Integer> list = new ArrayList<>();
                        list.add(nums[i]);
                        list.add(nums[j]);
                        list.add(nums[f]);
                        list.add(nums[t]);
                        rest.add(list);
                        f++;
                        while (f < t && nums[f] == nums[f-1]){
                            f++;
                        }
                        t--;
                        while (f < t && nums[t] == nums[t+1]){
                            t--;
                        }
                    }
                }
            }
        }
        return rest;
    }
}
//leetcode submit region end(Prohibit modification and deletion)
